﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace BatchModifyFilesTime
{
    public partial class FrmMain : Form
    {
        private HashSet<string> _files = new HashSet<string>();
        private Thread _thread;
        public FrmMain()
        {
            InitializeComponent();
        }

        private void timerCheck_Tick(object sender, EventArgs e)
        {
            if (chbTop.Checked)
            {
                if (!this.TopMost)
                { this.TopMost = true; }
            }
            else
            {
                if (this.TopMost)
                { this.TopMost = false; }
            }
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Multiselect = true;
            dlg.Filter = "图片文件(*.jpg;*.jpeg;*.png)|*.jpg;*.jpeg;*.png";
            if (dlg.ShowDialog() != DialogResult.OK) return;
            foreach (string fn in dlg.FileNames)
                _files.Add(fn);

            showMessage("添加成功");
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            lblMsg.Text = string.Empty;
        }

        public void showMessage(string msg)
        {
            lblMsg.Text = msg;
            lblMsg.Left = (this.Width - lblMsg.Width) / 2;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            _files.Clear();
            showMessage("清空完成");
        }

        private void btnExecute_Click(object sender, EventArgs e)
        {
            showMessage("");
            if (_files.Count == 0)
            {
                MessageBox.Show("文件数量为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            _thread = new Thread(workService);
            _thread.Start();
        }

        private void workService()
        {
            try
            {
                foreach (Control ctl in this.Controls) ctl.Enabled = false;
                this.Cursor = Cursors.WaitCursor;
                foreach (string fn in _files)
                {
                    if (File.Exists(fn))
                    {
                        File.SetCreationTime(fn, DateTime.Now);
                        File.SetLastWriteTime(fn, DateTime.Now);
                        File.SetLastAccessTime(fn, DateTime.Now);
                    }
                }
                showMessage("修改时间完成");
            }
            catch (ThreadAbortException) { }
            catch (Exception ex)
            {
                showMessage($"异常：{ex.Message}");
            }
            finally
            {
                foreach (Control ctl in this.Controls) ctl.Enabled = true;
                this.Cursor = Cursors.Default;
            }
        }
        static void AdjustBrightness(Bitmap bitmap, int brightness)
        {
            for (int y = 0; y < bitmap.Height; y++)
            {
                for (int x = 0; x < bitmap.Width; x++)
                {
                    // 获取当前像素的颜色
                    Color originalColor = bitmap.GetPixel(x, y);

                    // 计算新的颜色值，限制在0到255之间
                    int r = Math.Min(Math.Max(originalColor.R + brightness, 0), 255);
                    int g = Math.Min(Math.Max(originalColor.G + brightness, 0), 255);
                    int b = Math.Min(Math.Max(originalColor.B + brightness, 0), 255);

                    // 设置新的像素颜色
                    Color newColor = Color.FromArgb(r, g, b);
                    bitmap.SetPixel(x, y, newColor);
                }
            }
        }

        private void btnAdjustBrightness_Click(object sender, EventArgs e)
        {
            showMessage("");
            if (_files.Count == 0)
            {
                MessageBox.Show("文件数量为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            _thread = new Thread(adjustBrightnessService);
            _thread.Start();
        }
        private void adjustBrightnessService()
        {
            try
            {
                int value = (int)numValue.Value;
                foreach (Control ctl in this.Controls) ctl.Enabled = false;
                this.Cursor = Cursors.WaitCursor;
                progressBar1.Minimum = 0;
                progressBar1.Maximum = _files.Count;
                progressBar1.Value = 0;
                foreach (string fn in _files)
                {
                    progressBar1.Value += 1;

                    if (File.Exists(fn))
                    {
                        string tempfn=Path.GetTempFileName();   
                        File.Copy(fn, tempfn, true);

                        using (Bitmap bitmap = new Bitmap(tempfn))
                        {
                            AdjustBrightness(bitmap, value); // 调整亮度
                            bitmap.Save(fn);
                        }
                    }
                }
                showMessage("调整图片亮度完成");
            }
            catch (ThreadAbortException) { }
            catch (Exception ex)
            {
                showMessage($"异常：{ex.Message}");
            }
            finally
            {
                foreach (Control ctl in this.Controls) ctl.Enabled = true;
                this.Cursor = Cursors.Default;
            }
        }
    }
}
